package qiyu.im.core.server.handler;

import io.netty.channel.ChannelHandlerContext;
import qiyu.im.core.server.ImCoreHandlerFactory;
import qiyu.im.core.server.SimpleHandler;
import qiyu.im.core.server.constants.ImMsg;
import qiyu.im.core.server.enums.ImMsgCodeEnum;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-10-16
 * Time: 20:11
 */
public class ImCoreHandlerFactoryImpl implements ImCoreHandlerFactory {

    private static final Map<Integer, SimpleHandler> map = new HashMap<>();

    static {
        map.put(ImMsgCodeEnum.IM_BIZ_MSG.getCode(), new BiZiMsgHandler());
        map.put(ImMsgCodeEnum.IM_LOGIN_MSG.getCode(), new LoginMsgHandler());
        map.put(ImMsgCodeEnum.IM_LOGOUT_MSG.getCode(), new LogoutMsgHandler());
        map.put(ImMsgCodeEnum.IM_HEARTBEAT_MSG.getCode(), new HeartBeatMsgHandler());
    }

    @Override
    public void doHandler(ChannelHandlerContext ctx, ImMsg msg) {
        SimpleHandler simpleHandler = map.get(msg.getCode());
        if (simpleHandler == null) {
            // 记录一下
            ctx.close();
            return;
        }
        simpleHandler.doHandler(ctx,msg);
    }
}
